/**
 * 使用code来获取open_id
 * 返回open_id
 */

import axios from 'axios'
import { createRequire } from 'module'
import { resolve } from 'path'
import { mysqlObj } from '../../utils/mysql.js'
const require = createRequire(import.meta.url)
const express = require('express')

const vertifyRouter = express.Router()

vertifyRouter.get('/', function (req, res) {

    res.writeHead(200, { "Content-type": "text/json;charset=utf-8" })
    /**
     *请求微信小程序的接口所需要的数据
     */
    let url = "https://api.weixin.qq.com/sns/jscode2session"
    let appid = "wx34ad8806a17be6ec"
    let secret = "0a92537dc18c75243974018062812643"
    let js_code = req.query.code
    let grant_code = "authorization_code"

    // 返回给微信小程序的json字符串
    let json = { "session_key": "", "openid": "" };

    // 请求微信小程序的接口的url
    let reqUrl = `${url}?appid=${appid}&secret=${secret}&js_code=${js_code}&grant_code=${grant_code}`

    // 请求微信小程序的接口的方法

    axios.get(reqUrl).then((res) => {

        json.session_key = res.data.session_key
        json.openid = res.data.openid
        resolve();
    }).then(()=> {
        res.end(JSON.stringify(json));
    })
})

    export const vertify = vertifyRouter;